/*
 * State_Machine.h
 *
 *  Created on: Oct 2, 2024
 *      Author: Grasping
 */

#ifndef MY_INC_APP_APP_ACLOAD_VOLTSTATEMACHINE_H_
#define MY_INC_APP_APP_ACLOAD_VOLTSTATEMACHINE_H_
#include "MY_INC/APP/APP_ACLoad_Protection.h"
#include "MY_INC/APP/APP_ACLoad_Pwm.h"
#include "MY_INC/APP/APP_Init.h"
#include "MY_INC/LC/LC_SYS.h"

static inline void RunVoltLoop_State_ProcessControl(InterruptController *Controller);
static inline void Voltage_Current_Loop(InterruptController *InterruptController); // 电流环状态机主控函数
// static inline void RunVoltLoop_State_Stop(InterruptController *controller);        // 停止状态处理
static inline void RunVoltLoop_State_Idle(InterruptController *controller);          // 空闲状态处理
static inline void RunVoltLoop_State_WaitCrossZero(InterruptController *controller); // 等待过零状态处理
static inline void RunVoltLoop_State_Running(InterruptController *controller);       // 运行状态处理
static inline void RunVoltLoop_State_Machine(InterruptController *controller);       // 状态机主控函数

/*==================================状态机===================================*/
static inline void RunVoltLoop_State_ProcessControl(InterruptController *Controller)
{
    RunVoltLoop_State_Machine(Controller); // 运行电流环状态机
    SPWM_Control_Inline(&Controller->pwm); // 传递整个 controller 和 PWM控制
}

static inline void Voltage_Current_Loop(InterruptController *InterruptController)
{
    // 获取角度 (访问 LoopParameter 成员)
    InterruptController->param.phase_count = (InterruptController->param.phase_count + 1) % SIZE_SINE;
    InterruptController->param.theta = InterruptController->param.phase_count * SIZE_COEFFICIENT;
    Cordic_SinCos_Inline(InterruptController->param.theta, &InterruptController->param.sin_val,
                         &InterruptController->param.cos_val);
    VariableStep_Update_Inline(InterruptController->param.vref, InterruptController->param.vo_ref * _SQRT2 *1.01f);

    float temp =
        PMRdiff_Calculate_Inline(InterruptController->param.pr_v,
                                InterruptController->param.vref->currentTemp * InterruptController->param.sin_val,
                                InterruptController->param.vo_actual);
    InterruptController->pwm.mod_wave =
        (PRdiff_Calculate_Inline(InterruptController->param.pr_i[0], temp,
                                  InterruptController->param.il_actual) + // 主要处理三次谐波
         InterruptController->param.vo_actual) /
        InterruptController->param.udc_actual;

}
static inline void RunVoltLoop_State_Machine(InterruptController *controller)
{
    switch (controller->loop_sm.current_state) // 访问 loop_sm.current_state
    {
    case STATE_IDLE:                        // 枚举名称从 STATE_WAITCROSSZERO 改为 STATE_WAIT_CROSS_ZERO
        RunVoltLoop_State_Idle(controller); // 传递整个 controller
        break;
    case STATE_WAIT_CROSS_ZERO:                      // 枚举名称从 STATE_WAITCROSSZERO 改为 STATE_WAIT_CROSS_ZERO
        RunVoltLoop_State_WaitCrossZero(controller); // 传递整个 controller
        break;
    case STATE_RUN:                            // 枚举名称从 STATE_RUN 改为 STATE_RUN
        RunVoltLoop_State_Running(controller); // 传递整个 controller
        break;
    default: // 其他状态不处理

        break;
    }
}
// 空闲状态处理 (参数类型修正，内部访问 loop_sm)
static inline void RunVoltLoop_State_Idle(InterruptController *controller)
{
    switch (controller->loop_sm.event) // 访问 loop_sm.event
    {
    case EVENT_LOOP_NONE:                               // 无事件
        controller->loop_sm.current_state = STATE_IDLE; // 直接更新 current_state
        break;
    default: // 其他事件不响应
    	controller->loop_sm.current_state = STATE_IDLE;
        break;
    }
}

// 等待过零点状态处理 (参数类型修正，内部访问 controller->param 和 controller->loop_sm)
static inline void RunVoltLoop_State_WaitCrossZero(InterruptController *controller)
{
    switch (controller->loop_sm.event) // 访问 loop_sm.event
    {
    case EVENT_LOOP_NONE:                                     // 无事件
                                                              /*用户处理*/
        if ((controller->param.SignalAnalysisr_v->Vrms < 0.5f)) // 端口处无电压
        {
            LL_HRTIM_TIM_SetCompare1(HRTIM1, controller->pwm.timer[0], 0);
            LL_HRTIM_TIM_SetCompare1(HRTIM1, controller->pwm.timer[1], 0);
            controller->param.phase_count = 0; // 重置 phase_count
            controller->param.theta = 0;
            // 这里需要设置PWM的初始状态，初始状态不对会炸机
            controller->loop_sm.current_state = STATE_RUN; // 直接更新 current_state
            break;
        }
        /*默认转移判断*/
        controller->loop_sm.current_state = STATE_WAIT_CROSS_ZERO; // 默认切换到等待锁相状态
        break;
    default: // 其他事件不响应
    	controller->loop_sm.current_state = STATE_IDLE;
        break;
    }
}

/*---------- 运行状态处理 ----------*/
// 运行状态处理 (参数类型修正，内部访问 controller->param 和 controller->loop_sm)
static inline void RunVoltLoop_State_Running(InterruptController *controller)
{
    switch (controller->loop_sm.event) // 访问 loop_sm.event
    {
    case EVENT_LOOP_NONE: // 无事件
        Voltage_Current_Loop(controller);
        controller->pwm.pwm_en_flag = Activated; // 允许PWM
        /*默认转移*/
        controller->loop_sm.current_state = STATE_RUN; // 直接更新 current_state
        break;
    default: // 其他事件不响应
    	controller->loop_sm.current_state = STATE_IDLE;
        break;
    }
}

#endif /* INC_STATE_MACHINE_H_ */
